/**
 * 訂單查询

 */
var orgstatus;
function findlist(phone,orderid,roleId) {
	showDefault("loading","加载中...");
	var jsonstr = "filterscount=0&groupscount=0&pagenum=0&pagesize=1000&recordstartindex=0&recordendindex=1000&roleId="+roleId+"&phone="+phone+"&iphone="+phone+"&orderid="+orderid+"";
	$.ajax({
		cache: false,
		type: "post",
		datatype: 'json',
		contentType: 'application/json;charset=UTF-8',
		data: jsonstr,
		url: ''+httpipstr+'alfa-mobile-ws/rest/order/findlist?mobiletoken=' + getCookie("mobiletokensalesman") + '',
		success: function(data) {
			toast.hide();
			if(orderid==""){
				var tmpl = doT.template($("#dotTmpl").html());
				// 将数据扔进templ中。并在DIV中显示出来
				orgstatus=3;
				$("#dotmainhtml").html(tmpl(data.rows));
			}else{
				$("#orgname").html(data.rows[0].orgname);
				$("#address").html(data.rows[0].address);
				$("#num").html(data.rows[0].num+"桶");
				$("#createdDt").html("发布时间: "+data.rows[0].createdDt);
				$("#realnum").html((!data.rows[0].realnum ||data.rows[0].realnum=="undefined"? "0" : ""+data.rows[0].realnum+""))
				$("#xphone").html("联系方式: "+data.rows[0].iphone);
				setcookie("usercommentiphon", data.rows[0].phone, 300, "/");
				setcookie("orderno", data.rows[0].orderno, 300, "/");
				var json1 = '{"mobile": "'+data.rows[0].iphone+'"}';
				$.ajax({
						type: "post",
						datatype: 'json',
						contentType: 'application/json;charset=UTF-8',
						url: ''+httpipstr+'alfa-mobile-ws/rest/OpenId/GetSingleOpenIdForMobile?mobiletoken='+getCookie("mobiletokensalesman")+'',
						data: json1,
						success: function(data) {
							if(data.message=="1"){
								setcookie("usertotestopenid", data.data.openid, 300, "/");
							}else{
							}
						}
				})
				setcookie("userupdatatime", data.rows[0].createdDt, 300, "/");
				if(data.rows[0].orgstatus=="2"){
					orgstatus=3;
					$(".wait-time").html(data.rows[0].createdDt);
					$(".now").removeClass("addStatus");
					$(".finish").removeClass("addStatus");
				}else if(data.rows[0].orgstatus=="3"){
					orgstatus=4
					$(".wait-time").html(data.rows[0].createdDt);
					$(".now-time").html(data.rows[0].confirmDt);
					$(".wait").removeClass("addStatus");
					$(".finish").removeClass("addStatus");
										
				}else if(data.rows[0].orgstatus=="4"){
					$(".wait").removeClass("addStatus");
					$(".now").removeClass("addStatus");
					$(".finish").removeClass("addStatus");
					$(".sure").hide();
					$(".wait-time").html(data.rows[0].createdDt);
					$(".now-time").html(data.rows[0].confirmDt);
					$(".finish-time").html(data.rows[0].updatedDt);
					
				}
				
				
			}
		}
	});
}
/**
 * 订单确认
 * @returns
 */
function updateorders(orderid){
	var json = "";
	var url="";
	if(orgstatus=="3"){
		 json = {"orderid": ""+orderid+"","orgstatus":""+orgstatus+""};
		 url=""+httpipstr+"alfa-mobile-ws/rest/order/updateorders";
		 updateorderhttp(url,json)
	}else if(orgstatus=="4"){
		dialog.alert({
			title: "请确认收油桶数",
			msg: '',
			text:'',
			buttons: ['取消', '确定'],
			input:true
		}, function(ret) {
			if(ret.buttonIndex=="1"){
				toast.hide();
			}else if(ret.buttonIndex=="2"){
				newupdateorderhttp(ret.text,orderid)
			}
		    /*newupdateorderhttp(ret.text,orderid)*/
		})
		/* url=""+httpipstr+"alfa-mobile-ws/rest/order/updateorders";
		 updateorderwindows('#popupwindows',url,orderid,orgstatus);*/
		 
	}
		
}
function updateorderhttp(url,json){
	$.ajax({
		cache: false,
		type: "post",
		datatype: 'json',
		contentType: 'application/json;charset=UTF-8',
		url: ''+url+'?mobiletoken=' + getCookie("mobiletokensalesman") + '',
		data: JSON.stringify(json),
		success: function(data) {
			if(data.message=="1"){
				showDefault("loading","保存中...");
				location.reload() 
			}else{
				
			}
		}
	});
}
function onclickall(orderid){
	/*findlist(getCookie("iphone"), orderid, 9)*/
	location.href="../salesman/task-details.jsp?orderid="+orderid
}
function newupdateorderhttp(realnum,orderid){
	showDefault("loading","确认中...");
	var iphonetype=true;
	if(realnum==""){
		toast.hide();
		iphonetype=false;
		showDefault("fail","请输入实际收油桶数");
	}if(!realnum.match(/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/  )){
		toast.hide();
		iphonetype=false;
		showDefault("fail","请输入有效的桶数");
	}
	if(realnum.toString().length>5){
		toast.hide();
		iphonetype=false;
		showDefault("fail","请输入有效的桶数");
	}else if(iphonetype){
		var json = {"orderid": ""+orderid+"","orgstatus":"4","realnum":""+realnum+""};
		$.ajax({
			cache: false,
			type: "post",
			datatype: 'json',
			contentType: 'application/json;charset=UTF-8',
			url: ''+httpipstr+'alfa-mobile-ws/rest/order/updateorders?mobiletoken=' + getCookie("mobiletokensalesman") + '',
			data: JSON.stringify(json),
			success: function(data) {
				toast.hide();
				if(data.message=="1"){
					showDefault("loading","保存中...");
					var json2={"openid":""+getCookie("usertotestopenid")+"","time":""+getCookie("userupdatatime")+"","orderid":""+orderid+"","iphon":""+getCookie("usercommentiphon")+"","orderno":""+getCookie("orderno")+""}
					$.ajax({
						cache: false,
						type: "post",
						datatype: 'json',
						contentType: 'application/json;charset=UTF-8',
						url: 'http://tugo.cn/weixin_aef/recycle/TextToSleaman',
						data: JSON.stringify(json2),
						success: function(data) {
							if(data.errcode=="0"){
								toast.hide();
								location.reload();
							}
						}
					})
				}else{
					
				}
			}
		});
	}
	
}